#ifndef TASK_CURRENTTHREAD_H
#define TASK_CURRENTTHREAD_H

#include <string>
using std::string;

#include <cxxabi.h>
#include <execinfo.h>
#include <cstdlib>
#include <sys/syscall.h>
#include <unistd.h>

namespace CurrentThread {
    // internal
    extern __thread int t_cachedTid;
    extern __thread char t_tidString[32];
    extern __thread int t_tidStringLength;
    extern __thread const char *t_threadName;
    // 这里是使用__thread关键字, 这个关键字是GCC提供的线程局部存储设施
    // 用于修饰带有全局性, 并且值可能变化但是不值得用全局变量保护的值

    void cacheTid();

    inline int tid() {
        if (__builtin_expect(t_cachedTid == 0, 0)) cacheTid();
        return t_cachedTid;
    }

    inline const char *tidString() // for logging
    {
        return t_tidString;
    }

    inline int tidStringLength() // for logging
    {
        return t_tidStringLength;
    }

    inline const char *name() {
        return t_threadName;
    }

    bool isMainThread();


    string stackTrace(bool demangle);
}

#endif //TASK_CURRENTTHREAD_H
